package com.ble.demo.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

// 适配器：显示BLE指令，支持点击回写
public class BleCmdAdapter extends RecyclerView.Adapter<BleCmdAdapter.ViewHolder> {
    private final List<String> mCmdList;
    private final OnCmdClickListener mListener;

    // 点击回调接口
    public interface OnCmdClickListener {
        void onCmdClick(String cmd);
    }

    public BleCmdAdapter(List<String> cmdList, OnCmdClickListener listener) {
        this.mCmdList = cmdList;
        this.mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(android.R.layout.simple_list_item_1, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String rxItem = mCmdList.get(position);
        holder.tvCmd.setText(rxItem);
        holder.tvCmd.setTextColor(Color.parseColor("#999999"));
        holder.itemView.setOnClickListener(v -> mListener.onCmdClick(rxItem));
    }

    @Override
    public int getItemCount() {
        return mCmdList.size(); // 10条指令
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvCmd;
        ViewHolder(View itemView) {
            super(itemView);
            tvCmd = itemView.findViewById(android.R.id.text1); // 系统默认文本控件
        }
    }
}